﻿namespace Runoi.StockQuantBrain
{
    /// <summary>
    /// 量化策略模块
    /// </summary>
    public class MovingAverageStrategy
    {
        /// <summary>
        /// 判断是否发出买入信号
        /// </summary>
        /// <param name="prices">价格数据列表</param>
        /// <param name="shortPeriod">短期均线周期</param>
        /// <param name="longPeriod">长期均线周期</param>
        /// <returns>是否发出买入信号</returns>
        public bool IsBuySignal(IList<decimal> prices, int shortPeriod, int longPeriod)
        {
            var shortMA = CalculateMA(prices, shortPeriod); // 计算短期均线
            var longMA = CalculateMA(prices, longPeriod);  // 计算长期均线
            return shortMA > longMA; // 如果短期均线大于长期均线，发出买入信号
        }

        /// <summary>
        /// 计算移动平均线
        /// </summary>
        /// <param name="prices">价格数据列表</param>
        /// <param name="period">均线周期</param>
        /// <returns>移动平均线值</returns>
        private decimal CalculateMA(IList<decimal> prices, int period)
        {
            if (prices.Count < period) // 如果数据量不足，返回 0
                return 0;
            // 取最近的 period 个价格并计算平均值
            return prices.TakeLast(period).Average();
        }
    }
}
